import { reqGetGoodListData } from "@/api";

const state = {
  goodsListData: {},
};
const mutations = {
  SET_GOODSLISTDATA(state, goodsListData) {
    state.goodsListData = goodsListData;
  },
};
const actions = {
  async getGoodsListData({commit},searchParams) {
    try {
      const result = await reqGetGoodListData(searchParams);
      if (result.code === 200) {
        commit('SET_GOODSLISTDATA',result.data);
      }else {
        alert('请求成功，但获取商品列表失败');
      }
    } catch (error) {
      alert('获取商品列表请求失败');
    }
  },
};
const getters = {
  attrsList(state){
    // 如果获取undefined,返回空数组，防止报错
    return state.goodsListData.attrsList || [];
  },
  goodsList(state){
    return state.goodsListData.goodsList || [];
  },
  trademarkList(state) {
    return state.goodsListData.trademarkList || [];
  },
  total(state) {
    return state.goodsListData.total || 0;
  }
};

export default {
  state,
  mutations,
  actions,
  getters,
  //开启命名空间
  namespaced: true,
};
